<template>
  <div class="login-container__wrap">
    <el-form :model="formData" ref="form" class="demo-ruleForm">
      <el-form-item
        prop="loginName"
        :rules="[{ required: true, message: '请输入登录账号！' }]"
      >
        <el-input
          v-model="formData.loginName"
          autocomplete="off"
          placeholder="请输入登录账号"
        />
      </el-form-item>
      <el-form-item
        prop="password"
        :rules="[{ required: true, message: '请输入登录密码！' }]"
      >
        <el-input
          v-model="formData.password"
          type="password"
          show-password
          placeholder="请输入登录密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" :loading="loading"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      formData: {},
      loading: false
    };
  },
  methods: {
    login() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            this.loading = true;
            const { data } = await this.$api('user/fechLoginUser', {
              query: this.formData
            });
            this.$store.dispatch('user/login', data.accessToken);
            this.$router.push('/')
          } catch (err) {
            console.log(err);
          }
          this.loading = false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.login-container__wrap {
  width: 500px;
  margin: 30px auto;
}
</style>
